package org.metasketch.diagram.javafx.graphics;

import org.metasketch.editor.links.LineSection;
import org.metasketch.editor.timeline.SnapshotManager;
import org.metasketch.graphics.Point;

import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;

public class LineControlPoint extends Circle implements
		EventHandler<MouseEvent> {

	LineSection previousSection;
	LineSection nextSection;
	Point controlPoint;
	double radius = 5.0;

	// Point start;
	private double initX;
	private double initY;
	private Point2D dragAnchor;
	private boolean moving;

	public LineControlPoint(EventHandler<MouseEvent> handler) {
		setRadius(radius);
		setStyle("-fx-fill: blue; -fx-stroke: black; -fx-stroke-width: 1; ");
		setOnMousePressed(this);
		setOnMouseDragged(this);
		setOnMouseClicked(handler);

	}

	public void setLines(LineSection previous, LineSection next, Point point) {
		previousSection = previous;
		nextSection = next;
		controlPoint = point;

		if (previous != null)
			previous.setFinishControlPoint(this);

		if (next != null)
			next.setStartControlPoint(this);

		setTranslateX(controlPoint.getX());
		setTranslateY(controlPoint.getY());

	}

	public void setInitDragParams(double sceneX, double sceneY) {
		initX = getTranslateX();
		initY = getTranslateY();
		dragAnchor = new Point2D(sceneX, sceneY);
		moving = true;
	}

	public void move(double sceneX, double sceneY) {

		double dragX = sceneX - dragAnchor.getX();
		double dragY = sceneY - dragAnchor.getY();

		double newXPosition = initX + dragX;
		double newYPosition = initY + dragY;

		setPosition(newXPosition, newYPosition);

	}

	private void setPosition(double x, double y) {

		if (previousSection != null) {
			previousSection.setEndX(x);
			previousSection.setEndY(y);
			previousSection.refreshNodeLink();
		}

		if (nextSection != null) {
			nextSection.setStartX(x);
			nextSection.setStartY(y);
			nextSection.refreshNodeLink();
		}

		controlPoint.setX((int) x);
		controlPoint.setY((int) y);

		setTranslateX(x);
		setTranslateY(y);

	}

	@Override
	public void handle(MouseEvent event) {

		System.out.println("Ctrl Point: " + event);

		// can't move sections attached to nodes...
		if (previousSection == null && nextSection.getSource() != null) {
			event.consume();
			return;
		}

		if (nextSection == null && previousSection.getTarget() != null) {
			event.consume();
			return;
		}

		if (event.getEventType() == MouseEvent.MOUSE_PRESSED) {
			// SnapshotManager.takeSnapshot();
			setInitDragParams(event.getSceneX(), event.getSceneY());

		} else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {

			// System.out.println(event);

			move(event.getSceneX(), event.getSceneY());

		}
		// this should work, if not for the fact that it never gets detected
		// over the control point, but the line...very weird...
		// else if (event.getEventType() == MouseEvent.MOUSE_RELEASED && moving)
		// {
		// moving = false;
		// SnapshotManager.takeSnapshot();
		// }

		event.consume();

	}

	public Point getControlPoint() {
		return controlPoint;
	}

}
